package lesson20;

import java.util.ArrayList;
import java.util.concurrent.*;

public class MyFutureTask2 {

    public static void main(String[] args) {
       ExecutorService executorService = Executors.newFixedThreadPool(10);
       ArrayList<Future> allFutures = new ArrayList<>();
       for(int i = 0; i < 4; i ++){
           if(i % 2 == 0){
               allFutures.add(executorService.submit(new FastTask()));
           }else{
               allFutures.add(executorService.submit(new SlowTask()));
           }
       }

       allFutures.parallelStream().forEach(future -> {
           try {
               System.out.println(future.get());
           } catch (InterruptedException e) {
               e.printStackTrace();
           } catch (ExecutionException e) {
               e.printStackTrace();
           }
       });



        /*for(Future future : allFutures){
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }*/

        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

    static class FastTask implements Callable<String>{
        @Override
        public String call() throws Exception {
            return Thread.currentThread().getName() + " : 速度快的任务...";
        }
    }

    static class SlowTask implements Callable<String>{
        @Override
        public String call() throws Exception {
            Thread.sleep(1000);
            return Thread.currentThread().getName() + " : 速度慢的任务...";
        }
    }
}
